<template>
    <div>
        <el-container>
            <el-header>
                <img class="mlogo" src="../assets/logo.png" alt="头像"/>
            </el-header>
            <el-main>
                <el-form
                        :model="ruleForm"
                        :rules="rules"
                        ref="ruleForm"
                        label-width="100px"
                        class="demo-ruleForm"
                >
                    <el-form-item label="用户名" prop="username">
                        <el-input v-model="ruleForm.username"></el-input>
                    </el-form-item>
                    <!--prop属性绑定字段名name，表单验证时，就会验证el-input元素绑定的变量-->
                    <el-form-item label="密码" prop="password">
                        <el-input type="password" v-model="ruleForm.password"></el-input>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="submitForm('ruleForm')"
                        >登录
                        </el-button
                        >
                        <el-button @click="resetForm('ruleForm')">重置</el-button>
                    </el-form-item>
                </el-form>
            </el-main>
        </el-container>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                ruleForm: {
                    username: "markerhub",
                    password: "111111",
                },
                rules: {
                    username: [
                        {required: true, message: "请输入用户名", trigger: "blur"},
                        {
                            min: 3,
                            max: 15,
                            message: "长度在 3 到 15 个字符",
                            trigger: "blur",
                        },
                    ],
                    password: [
                        {required: true, message: "请输入密码", trigger: "blur"},
                        {
                            min: 6,
                            max: 12,
                            message: "长度在 6 到 12 个字符",
                            trigger: "blur",
                        },
                    ],
                },
            };
        },

        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        const _this = this;
                        //登录
                        this.$axios
                            .post("http://localhost:8081/login", this.ruleForm)
                            .then((res) => {
                                console.log("res.data");
                                console.log(res.data);

                                // 拿到请求头中的jwt
                                const jwt = res.headers["authorization"];

                                // 拿到date信息
                                const userInfo = res.data.data;

                                // 从sessionStorage和localStorage获取信息
                                _this.$store.commit("SET_TOKEN", jwt);
                                _this.$store.commit("SET_USERINFO", userInfo);

                                // 获取用户信息
                                console.log("_this.$store.getters.getUser"+ _this.$store.getters.getUser);

                                // 跳转到详情页面
                                _this.$router.push("/blog/blogs");
                            });
                    } else {
                        console.log("error submit!!");
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
        },
        components: {},

        computed: {},
    };
</script>
<style lang="less" scoped>
    .el-header,
    .el-footer {
        background-color: #b3c0d1;
        color: #333;
        text-align: center;
        line-height: 60px;
    }

    .el-aside {
        background-color: #d3dce6;
        color: #333;
        text-align: center;
        line-height: 200px;
    }

    .el-main {
        background-color: #e9eef3;
        color: #333;
        text-align: center;
        line-height: 160px;
    }

    body > .el-container {
        margin-bottom: 40px;
    }

    .el-container:nth-child(5) .el-aside,
    .el-container:nth-child(6) .el-aside {
        line-height: 260px;
    }

    .el-container:nth-child(7) .el-aside {
        line-height: 320px;
    }

    .mlogo {
        height: 90%;
        margin-top: 10px;
    }

    .demo-ruleForm {
        max-width: 400px;
        margin: 0 auto;
    }
</style>
